package com.liuyi.framework.factory;


import com.liuyi.tools.web.unusual.BizEnum;
import com.liuyi.tools.web.unusual.BizException;

import java.io.Serializable;
import java.util.Map;

/**
 * @author Mr.fmy
 * @version 1.0.0
 * @ClassName SpringTypeSingFactory
 * @Description TODO
 * @createTime 2020/3/18 0018 11:09
 */
public class SpringTypeStaticFactory {


    @SuppressWarnings("unchecked")
    public static <T extends SpringTypeFactory<T, R>, R extends Serializable> T get(Class<T> tClass, R r) {
        Map<Class, Map<String, ? super SpringTypeFactory>> vs = SpringTypeFactoryHolder.getVs();
        if (!vs.containsKey(tClass)) {
            throw new BizException(BizEnum.BUSINESS_FAIL, "抱歉未找到：" + tClass + " 相关继承类");
        }
        Map<String, ? super SpringTypeFactory<?, ? extends Serializable>> v = vs.get(tClass);
        for (String k : v.keySet()) {
            Object o = v.get(k);
            if (tClass.isInstance(o)) {
                T i = (T) v.get(k);
                if (i.is(r)) {
                    return i;
                }
            }
        }
        throw new BizException(BizEnum.BUSINESS_FAIL, "抱歉未找到：" + r.getClass() + " 相关处理类");
    }

}
